export function genKey(key) {
  return key;
}

export function save(key, value) {
  console.log(`save key=${key}, value=${value}`);
  if (value === null || value === undefined) {
    return;
  }
  let saveValue;
  if (value.toJS) {
    saveValue = JSON.stringify(value.toJS());
  } else if (typeof value === 'string' || value instanceof String) {
    saveValue = value;
  } else {
    saveValue = JSON.stringify(value);
  }
  try {
    localStorage.setItem(genKey(key), saveValue);
  } catch (error) {
    // eslint-disable-next-line
    alert('请关闭您浏览器的无痕浏览后再使用');
  }
}

export function get(key) {
  console.log('get ', key);
  let str;
  try {
    str = localStorage.getItem(genKey(key));
  } catch (e) {
    // eslint-disable-next-line
    alert('请关闭您浏览器的无痕浏览后再使用');
  }
  let value;
  if (str) {
    try {
      value = JSON.parse(str);
    } catch (e) {
      value = str;
    }
  } else {
    value = null;
  }
  console.log(`get key=${key}, value=`, value);
  return value;
}

export function clear(key) {
  try {
    localStorage.removeItem(genKey(key));
  } catch (error) {
    // eslint-disable-next-line
    alert('请关闭您浏览器的无痕浏览后再使用');
  }
}
